package cn.com.gridinfo.sooc.core.expand.feedback.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.feedback.CodeConst;
import cn.com.gridinfo.sooc.core.expand.feedback.adapter.SuggestionInfoAdapter;
import cn.com.gridinfo.sooc.core.expand.feedback.dao.SuggestionDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 反馈详情界面
 * Created by yingchunli on 2015/12/23.
 */
public class FeedbackInfoActivity extends BaseSoocActivity {

    @InjectView(R.id.suggestion_list)
    UltimateRecyclerView suggestion_list;//回复反馈的列表
    LinearLayoutManager linearLayoutManager;//布局管理器
    SuggestionInfoAdapter adapter;//适配器
    SuggestionDao dao;
    int maxLastPositon;//数据最后位置
    @InjectView(R.id.content)
    TextView content;//反馈内容
    @InjectView(R.id.uname)
    TextView uname;//发布人
    @InjectView(R.id.time)
    TextView time;//发布时间
    @InjectView(R.id.hfcount)
    TextView hfcount;//回复个数
    @InjectView(R.id.btn_send)
    TextView btnSend;//确定按钮
    @InjectView(R.id.nr)
    EditText nr;//回复内容
    String ztid;//反馈id

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_feedbackinfo);
        ButterKnife.inject(this);
        dao = new SuggestionDao(this);
        linearLayoutManager = new LinearLayoutManager(this);
        suggestion_list.setLayoutManager(linearLayoutManager);
        PageConst.pageNo = 1;
        PageConst.pageSize = 10;
        //获取反馈id
        ztid = getIntent().getStringExtra("id");
        //获取列表数据  首次请求
        dao.getSuggestionInfo(ztid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_FEEDBACK_SUGGESTIONINFO_FIRST);

        //按键完成监听
        nr.setImeOptions(EditorInfo.IME_ACTION_DONE);
        nr.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    onClick();
                }
                return false;
            }
        });
    }

    /**
     * 初始化反馈详情列表数据
     */
    void initView() {
        adapter = new SuggestionInfoAdapter(dao.getSuggestion().getReplylist());
        suggestion_list.setAdapter(adapter);
        suggestion_list.enableLoadmore();
        suggestion_list.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                PageConst.pageNo = 1;
                dao.getSuggestionInfo(ztid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_FEEDBACK_SUGGESTIONINFO_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        suggestion_list.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                if (itemsCount > (PageConst.pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                    dao.getSuggestionInfo(ztid, ++PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_FEEDBACK_SUGGESTIONINFO_PULLUP);
                } else {
                    adapter.loadNoDataView();
                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                }
            }
        });
    }

    /**
     * 请求成功
     */
    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.EXPAND_FEEDBACK_SUGGESTIONINFO_FIRST:
                        //获取反馈详情列表返回成功
                        content.setText(dao.getSuggestion().getZtnr());
                        time.setText(dao.getSuggestion().getInput_time());
                        hfcount.setText(dao.getSuggestion().getReplycount());
                        uname.setText(dao.getSuggestion().getUname());
                        initView();
                        break;
                    case CodeConst.EXPAND_FEEDBACK_SUGGESTIONINFO_PUSHDOWN:
                        //刷新数据
                        suggestion_list.setRefreshing(false);
                        hfcount.setText(dao.getSuggestion().getReplycount());

                        adapter.refresh(dao.getSuggestion().getReplylist());

                        if (dao.getSuggestion().getReplylist().size() > (PageConst.pageSize - 1))
                            //数据大于每页显示的条数,则重新开启加载更多
                            adapter.loadMoreDataView();
                        else {
                            //数据小于当前页的话则不显示加载更多
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;
                    case CodeConst.EXPAND_FEEDBACK_SUGGESTIONINFO_PULLUP:
                        //下拉加载
                        if (dao.getSuggestion().getReplylist() != null && !dao.getSuggestion().getReplylist().isEmpty()) {
                            //加载更多
                            adapter.loadmore(dao.getSuggestion().getReplylist());
                        } else {
                            //无数据则不显示下一页
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                    case CodeConst.EXPAND_FEEDBACK_HF:
                        //回复反馈成功
                        MessageUtils.showRightImageShortToast(this, "回复成功");
                        nr.setText("");
                        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(nr.getWindowToken(), 0);
                        int hf = Integer.parseInt(hfcount.getText().toString().trim()) + 1;
                        hfcount.setText(hf + "");
                        PageConst.pageNo = 1;
                        dao.getSuggestionInfo(ztid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_FEEDBACK_SUGGESTIONINFO_PUSHDOWN);

                        break;
                }
            } else {
                MessageUtils.showRightImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showRightImageShortToast(this, "数据请求异常");
        }

    }

    /**
     * 输入框内容为空时，确定按钮不可点击
     */
    @OnTextChanged(value = {R.id.nr}, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAfterTextChanged() {
        if (nr.getText().toString().trim().equals("")) {
            btnSend.setEnabled(false);
        } else {
            btnSend.setEnabled(true);
        }
    }

    /**
     * 确定按钮的点击
     */
    @OnClick(R.id.btn_send)
    void onClick() {
        dao.hfSuggestion(ztid, nr.getText().toString().trim());
    }


    @Override
    public String setupToolBarTitle() {
        return "反馈详情";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolBarLeftClick() {
        Intent intent = new Intent();
        intent.putExtra("hfcount", hfcount.getText().toString().trim());
        setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * 点击手机的返回键时
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Intent intent = new Intent();
        intent.putExtra("hfcount", hfcount.getText().toString().trim());
        setResult(RESULT_OK, intent);
        return super.onKeyDown(keyCode, event);
    }
}
